package gui.screens;

import gui.CanvasManager;
import gui.layer.ArrowTiledLayer;
import gui.layer.BaseLayer;
import gui.layer.GreenTiledLayer;
import gui.layer.HunterSprite;
import gui.layer.RabbitTiledLayer;
import gui.menu.GameMenu;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

import application.MainController;

/**
 * Class for the game field.
 * @author Sandra Mayer
 *
 */
public class RBCanvas extends GameCanvas implements Runnable {

	private GreenTiledLayer m_Grass = null;
	private RabbitTiledLayer m_Rabbits = null;
//	private PowerUpsTiledLayer m_PowerUps = null;
	private ArrowTiledLayer m_Arrows = null;
	private HunterSprite m_Hunter = null;

	// the center of the screen
	public final int CENTER_X = getWidth() / 2;
	public final int CENTER_Y = getHeight() / 2;

	/**
	 * Constructor.
	 */
	public RBCanvas() {
		super(true);
		CanvasManager.setCurScreen(this);
		CanvasManager.setRBCanvasToMidlet(this);
		MainController.getInstance().startUpdateIntervall();
		try {
			m_Grass = new GreenTiledLayer();
			m_Rabbits = new RabbitTiledLayer();
//			m_PowerUps = new PowerUpsTiledLayer();
			m_Arrows = new ArrowTiledLayer(getGraphics());
			m_Hunter = new HunterSprite();
			this.setCommandListener(new GameMenu(this));
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		Thread runner = new Thread(this);
		runner.start();
	}
	
	/**
	 * Starts the gamefield.
	 */
	public void run() {
		updateGameScreen(getGraphics());
	}

	/**
	 * Update the game screen.
	 * @param g Graphics
	 */
	private void updateGameScreen(Graphics g) {
		// the next two lines clear the background
		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());

		buildGameScreen(g);

		// this call paints off screen buffer to screen
		flushGraphics();
	}

	/**
	 * Builds the game screen.
	 * @param g Graphics
	 */
	private void buildGameScreen(Graphics g) {
		m_Grass.paint(g);
		m_Rabbits.paint(g);
//		m_PowerUps.paint(g);
		m_Arrows.paint(g);
		m_Hunter.paint(g);

		// set the drawing color to black
		g.setColor(0x000000);

		for (int i = BaseLayer.SIZEOFCELL; i < BaseLayer.NROFCELLSVER
				* BaseLayer.SIZEOFCELL; i = i + BaseLayer.SIZEOFCELL) {
			g.drawLine(0, i, BaseLayer.GAME_WIDTH, i);
		}

		for (int i = BaseLayer.SIZEOFCELL; i < BaseLayer.NROFCELLSHOR
				* BaseLayer.SIZEOFCELL; i = i + BaseLayer.SIZEOFCELL) {
			g.drawLine(i, 0, i, BaseLayer.GAME_HEIGHT);
		}
	}

	/**
	 * Updates the game field.
	 * @param posOfRabbits position of the rabbits
	 * @param posOfPowerUps position of the power ups
	 * @param posOfArrows position of the arrows
	 */
	public void update(int[] posOfRabbits,
			int[] posOfPowerUps, int[] posOfArrows) {
		if (posOfRabbits != null)
			m_Rabbits.update(posOfRabbits);
		if (posOfPowerUps != null)
//			m_PowerUps.update(posOfPowerUps);
		if (posOfArrows != null)
			m_Arrows.update(posOfArrows);
		updateGameScreen(getGraphics());
	}

	/**
	 * Updates the text that is shown with the arrows
	 * @param nrOfArrow the number of the arrow where the text should be
	 * @param amountOfRabbits the amount of rabbits
	 */
	public void updateGraphics(int nrOfArrow, int amountOfRabbits) {
		m_Arrows.updateGraphics(nrOfArrow, amountOfRabbits);
		repaint();
	}
	
	/**
	 * Destroys the application.
	 * @param destroy boolean
	 */
	public void destroyApp(boolean destroy) {
		CanvasManager.destroyApp();
	}
}